<template>
  <div class="parallax-swiper-container">
    <swiper class="parallax-swiper" :style="swiperStyle" :direction="'vertical'" :pagination="pagination"
      :slidesPerView="1" :spaceBetween="0" :mousewheel="{ sensitivity: 1 }" :modules="modules" :speed="1000"
      :parallax="true" @swiper="onSwiper" @slideChange="onSlideChange">
      <swiper-slide>
        <div class="slide-container">
          <!-- 海洋背景 - 使用更广阔的海景 -->
          <div class="parallax-bg" data-swiper-parallax="100%">
            <video class="w-full h-full object-cover" src="https://vjs.zencdn.net/v/oceans.mp4" autoplay loop
              muted></video>
          </div>

          <div class="parallax-content" data-swiper-parallax="0%">
            <h1 data-swiper-parallax-opacity="0" data-swiper-parallax-y="50" data-swiper-parallax-scale="0.8">海洋深处</h1>
            <p data-swiper-parallax-opacity="0" data-swiper-parallax-y="100" data-swiper-parallax-delay="200">
              潜入蓝色世界，探索未知的海洋奥秘
            </p>
          </div>
        </div>
      </swiper-slide>
      <!-- 第一屏：森林视差效果 -->
      <swiper-slide>
        <div class="slide-container">
          <!-- 背景层 - 视差效果 -->
          <div class="parallax-bg" data-swiper-parallax="100%">
            <img class="w-full h-full object-cover scale-110" src="https://picsum.photos/id/560/1920/1080" alt="森林背景">
          </div>

          <!-- 前景层 -->
          <div class="parallax-content" data-swiper-parallax="0%">
            <h1 data-swiper-parallax-opacity="0" data-swiper-parallax-y="50" data-swiper-parallax-scale="0.8">自然之美</h1>
            <p data-swiper-parallax-opacity="0" data-swiper-parallax-y="100" data-swiper-parallax-delay="200">
              探索地球上最壮丽的景观，感受大自然的神奇魅力
            </p>
          </div>
        </div>
      </swiper-slide>

      <!-- 第二屏：海洋视差效果 -->
      <swiper-slide>
        <div class="slide-container">
          <!-- 海洋背景 - 使用更广阔的海景 -->
          <div class="parallax-bg" data-swiper-parallax="100%">
            <img class="w-full h-full object-cover scale-110" src="https://picsum.photos/id/446/1920/1080" alt="广阔海洋背景">
          </div>

          <div class="parallax-content" data-swiper-parallax="0%">
            <h1 data-swiper-parallax-opacity="0" data-swiper-parallax-y="50" data-swiper-parallax-scale="0.8">海洋深处</h1>
            <p data-swiper-parallax-opacity="0" data-swiper-parallax-y="100" data-swiper-parallax-delay="200">
              潜入蓝色世界，探索未知的海洋奥秘
            </p>
          </div>
        </div>
      </swiper-slide>

      <!-- 第三屏：山脉视差效果 -->
      <swiper-slide>
        <div class="slide-container">
          <div class="parallax-bg" data-swiper-parallax="50%">
            <img class="w-full h-full object-cover scale-110" src="https://picsum.photos/id/1018/1920/1080" alt="山脉背景">
          </div>

          <div class="parallax-content" data-swiper-parallax="0%">
            <h1 data-swiper-parallax-opacity="0" data-swiper-parallax-y="50" data-swiper-parallax-scale="0.8">高山之巅</h1>
            <p data-swiper-parallax-opacity="0" data-swiper-parallax-y="100" data-swiper-parallax-delay="200">
              攀登险峻高峰，俯瞰壮丽山河
            </p>
          </div>
        </div>
      </swiper-slide>

      <!-- 第四屏：城市视差效果 -->
      <swiper-slide>
        <div class="slide-container">
          <!-- 城市背景 -->
          <div class="parallax-bg" data-swiper-parallax="50%">
            <img class="w-full h-full object-cover scale-110" src="https://picsum.photos/id/1031/1920/1080" alt="城市背景">
          </div>

          <div class="parallax-content" data-swiper-parallax="0%">
            <h1 data-swiper-parallax-opacity="0" data-swiper-parallax-y="50" data-swiper-parallax-scale="0.8">都市繁华</h1>
            <p data-swiper-parallax-opacity="0" data-swiper-parallax-y="100" data-swiper-parallax-delay="200">
              穿梭于钢筋水泥森林，感受城市脉搏的跳动
            </p>
          </div>
        </div>
      </swiper-slide>

      <!-- 第五屏：沙漠视差效果 -->
      <swiper-slide>
        <div class="slide-container">
          <!-- 沙漠背景 -->
          <div class="parallax-bg" data-swiper-parallax="50%">
            <img class="w-full h-full object-cover scale-110" src="https://picsum.photos/id/185/1920/1080" alt="沙漠背景">
          </div>

          <div class="parallax-content" data-swiper-parallax="0%">
            <h1 data-swiper-parallax-opacity="0" data-swiper-parallax-y="50" data-swiper-parallax-scale="0.8">金色沙漠</h1>
            <p data-swiper-parallax-opacity="0" data-swiper-parallax-y="100" data-swiper-parallax-delay="200">
              漫步于金色沙海，体验大自然的苍凉与壮美
            </p>
          </div>
        </div>
      </swiper-slide>

      <!-- 自定义导航按钮 -->
      <template #container-end>
        <div class="swiper-navigation">
          <el-button class="swiper-button-prev" @click="prevSlide">
            <el-icon>
              <ArrowLeft />
            </el-icon>
          </el-button>
          <el-button class="swiper-button-next" @click="nextSlide">
            <el-icon>
              <ArrowRight />
            </el-icon>
          </el-button>
        </div>

        <!-- 滚动指示器 -->
        <div class="scroll-hint">
          <span>向下滚动</span>
          <div class="scroll-icon"></div>
        </div>
      </template>
    </swiper>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'
import 'swiper/css/parallax'  // 添加parallax样式导入
import { Mousewheel, Parallax, Navigation, Pagination } from 'swiper/modules'

// Swiper 模块配置
const modules = ref([Mousewheel, Parallax, Navigation, Pagination])
const pagination = ref({
  clickable: true,
  // type: 'fraction',
  // type: 'progressbar',
  type: 'bullets',
})
// Swiper 实例引用
let swiperInstance = null

// 获取 Swiper 实例
const onSwiper = (swiper) => {
  swiperInstance = swiper
  // 初始化时手动触发一次视差计算
  setTimeout(() => {
    if (swiperInstance) {
      swiperInstance.update()
    }
  }, 100)
}

// 幻灯片切换事件
const onSlideChange = (swiper) => {
  // console.log('当前幻灯片索引:', swiper.activeIndex)
  // 切换时重新计算视差
  swiper.update()
}

// 上一张幻灯片
const prevSlide = () => {
  if (swiperInstance) {
    swiperInstance.slidePrev()
  }
}

// 下一张幻灯片
const nextSlide = () => {
  if (swiperInstance) {
    swiperInstance.slideNext()
  }
}

// 窗口大小改变时更新Swiper
const handleResize = () => {
  if (swiperInstance) {
    swiperInstance.update()
  }
}

onMounted(() => {
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})

// Swiper 样式
const swiperStyle = {
  width: '100%',
  height: '100vh',
  '--swiper-navigation-color': '#f00',
  '--swiper-pagination-color': '#fff',
  '--swiper-pagination-bullet-inactive-color': '#fff',
  '--swiper-pagination-bullet-inactive-opacity': '0.5',
  '--swiper-parallax-bg-opacity': '0.1', // 调整视差背景透明度
}
</script>

<style scoped lang="scss">
.parallax-swiper-container {
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.parallax-swiper {
  width: 100%;
  height: 100%;
  perspective: 1200px; // 增加透视效果
}

.slide-container {
  width: 100%;
  height: 100vh;
  position: relative;
  overflow: hidden;
  transform-style: preserve-3d; // 保留3D效果
}

.parallax-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  // 添加模糊效果增强深度感
  filter: blur(0px);
}

.parallax-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 2rem;
  color: white;
  pointer-events: none;
  // mix-blend-mode: difference;
  // filter: invert(1) hue-rotate(180deg);
}

.parallax-content h1 {
  font-size: 4rem;
  font-weight: 700;
  margin-bottom: 1.5rem;
  text-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
  max-width: 80%;
}

.parallax-content p {
  font-size: 1.5rem;
  line-height: 1.6;
  max-width: 70%;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

/* 增强视差元素的动画过渡效果 */
.parallax-content h1,
.parallax-content p {
  transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.swiper-slide:nth-child(3) .parallax-bg img {
  animation: starTwinkle 1s ease-in-out infinite alternate;
}

@keyframes starTwinkle {
  0% {
    opacity: 0.8;
  }

  100% {
    opacity: 1;
  }
}

.swiper-slide:nth-child(2) .parallax-bg img {
  animation: auroraFlow 10s ease-in-out infinite alternate;
}

@keyframes auroraFlow {
  0% {
    transform: translateX(0) translateY(0);
  }

  100% {
    transform: translateX(2.5%) translateY(2.5%);
  }
}

/* 自定义导航按钮 */
.swiper-navigation {
  position: absolute;
  left: 50%;
  bottom: 4rem;
  transform: translateX(-50%);
  z-index: 10;
}

.swiper-button-prev,
.swiper-button-next {
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(4px);
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  color: white;
  font-size: 1.5rem;
  transform: rotate(-90deg);
}

.swiper-button-prev:hover {
  background-color: rgba(255, 255, 255, 0.4);
  transform: scale(1.1) rotate(-90deg);
}

.swiper-button-next:hover {
  background-color: rgba(255, 255, 255, 0.4);
  transform: scale(1.1) rotate(-90deg);
}

/* 滚动指示器 */
.scroll-hint {
  position: absolute;
  bottom: 10rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  text-align: center;
  color: white;
  animation: fadeInUp 2s ease-in-out infinite;
}

.scroll-hint span {
  display: block;
  font-size: 1rem;
  margin-bottom: 0.5rem;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.scroll-icon {
  width: 20px;
  height: 30px;
  border: 2px solid white;
  border-radius: 15px;
  margin: 0 auto;
  position: relative;
}

.scroll-icon::before {
  content: '';
  position: absolute;
  top: 5px;
  left: 50%;
  width: 4px;
  height: 8px;
  background-color: white;
  border-radius: 2px;
  transform: translateX(-50%);
  animation: scrollDown 1.5s ease-in-out infinite;
}

@keyframes scrollDown {
  0% {
    top: 5px;
    opacity: 1;
  }

  100% {
    top: 15px;
    opacity: 0;
  }
}

@keyframes fadeInUp {

  0%,
  100% {
    opacity: 0.6;
    transform: translateX(-50%) translateY(10px);
  }

  50% {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .parallax-content h1 {
    font-size: 2.5rem;
  }

  .parallax-content p {
    font-size: 1.2rem;
  }
}
</style>